class_name ResourceTracker
extends Resource

# 资源追踪器 - 专门追踪能量、卡牌等资源相关的统计
# 职责：记录资源使用效率、流转情况等

signal resource_changed(resource_type: String, old_value: int, new_value: int)

# 资源统计数据
var mana_efficiency: float = 0.0  # 能量使用效率
var cards_per_mana: float = 0.0   # 每点能量使用的卡牌数
var mana_waste: int = 0           # 浪费的能量
var optimal_plays: int = 0        # 最优使用次数
var resource_history: Array[Dictionary] = []
var max_history_size: int = 30

# 回合开始时的资源状态
var turn_start_mana: int = 0
var turn_start_cards: int = 0

func _init():
	reset_turn_data()

# 重置回合数据
func reset_turn_data() -> void:
	mana_efficiency = 0.0
	cards_per_mana = 0.0
	mana_waste = 0
	optimal_plays = 0
	resource_history.clear()
	turn_start_mana = 0
	turn_start_cards = 0

# 设置回合开始状态
func set_turn_start_state(mana: int, cards: int) -> void:
	turn_start_mana = mana
	turn_start_cards = cards

# 追踪能量事件
func track_mana_event(old_mana: int, new_mana: int) -> void:
	var event_data = {
		"type": "mana_change",
		"old_value": old_mana,
		"new_value": new_mana,
		"change": new_mana - old_mana,
		"timestamp": Time.get_unix_time_from_system()
	}
	
	_add_to_history(event_data)
	
	if new_mana < old_mana:
		# 能量消耗
		_calculate_mana_efficiency(old_mana - new_mana)
	elif new_mana > old_mana:
		# 能量获得
		pass
	
	resource_changed.emit("mana", old_mana, new_mana)

# 追踪卡牌使用
func track_card_played(card_cost: int, card_effect_value: int = 1) -> void:
	var event_data = {
		"type": "card_played",
		"cost": card_cost,
		"effect_value": card_effect_value,
		"efficiency": float(card_effect_value) / max(1, card_cost),
		"timestamp": Time.get_unix_time_from_system()
	}
	
	_add_to_history(event_data)
	
	# 评估是否为最优使用
	if event_data["efficiency"] >= 1.5:  # 效率阈值
		optimal_plays += 1

# 计算能量使用效率
func _calculate_mana_efficiency(mana_spent: int) -> void:
	var total_mana_used = 0
	var total_cards_played = 0
	
	for event in resource_history:
		if event["type"] == "mana_change" and event["change"] < 0:
			total_mana_used += abs(event["change"])
		elif event["type"] == "card_played":
			total_cards_played += 1
	
	if total_mana_used > 0:
		cards_per_mana = float(total_cards_played) / float(total_mana_used)
		mana_efficiency = min(1.0, cards_per_mana / 0.5)  # 理想值为每点能量使用0.5张卡

# 计算回合结束时的资源浪费
func calculate_turn_end_waste(remaining_mana: int) -> void:
	mana_waste += remaining_mana
	
	var waste_event = {
		"type": "mana_waste",
		"amount": remaining_mana,
		"timestamp": Time.get_unix_time_from_system()
	}
	_add_to_history(waste_event)

# 添加事件到历史记录
func _add_to_history(event_data: Dictionary) -> void:
	resource_history.append(event_data)
	if resource_history.size() > max_history_size:
		resource_history.pop_front()

# 检查资源条件
func check_resource_condition(condition_data: Dictionary) -> bool:
	var condition_type = condition_data.get("condition_type", "")
	var threshold = condition_data.get("threshold", 0.0)
	
	match condition_type:
		"mana_efficiency":
			return mana_efficiency >= threshold
		"cards_per_mana":
			return cards_per_mana >= threshold
		"mana_waste":
			return mana_waste <= threshold  # 浪费越少越好
		"optimal_plays":
			return optimal_plays >= threshold
		_:
			print("❌ 未知资源条件类型: %s" % condition_type)
			return false

# 获取资源统计
func get_resource_stats() -> Dictionary:
	return {
		"mana_efficiency": mana_efficiency,
		"cards_per_mana": cards_per_mana,
		"mana_waste": mana_waste,
		"optimal_plays": optimal_plays,
		"history_size": resource_history.size()
	}

# 打印资源统计
func print_resource_stats() -> void:
	print("  能量效率: %.2f" % mana_efficiency)
	print("  卡牌/能量比: %.2f" % cards_per_mana)
	print("  浪费能量: %d" % mana_waste)
	print("  最优使用: %d" % optimal_plays)

# 获取资源使用建议
func get_resource_advice() -> String:
	if mana_efficiency < 0.3:
		return "建议提高能量使用效率，考虑使用更多低费卡牌"
	elif mana_waste > 5:
		return "注意减少能量浪费，尽量在回合结束前用完能量"
	elif optimal_plays < 2:
		return "寻找更多高效率的卡牌组合"
	else:
		return "资源使用良好，继续保持！" 
